package com.gjx.clock.dao

import android.content.Context
import androidx.core.content.edit
import com.gjx.clock.ClockApplication
import com.gjx.clock.entity.RestAlarmClock
import com.google.gson.Gson

object RestClockSettingDao {
    private fun sharedPreferences() = ClockApplication.context.
        getSharedPreferences("restAlarmSetting", Context.MODE_PRIVATE)

    fun setRestAlarm(clock: RestAlarmClock){
        sharedPreferences().edit {
            putString("clock",Gson().toJson(clock))
        }
    }

    fun getSavedClock(): RestAlarmClock {
        if(sharedPreferences().contains("clock")){
            val String = sharedPreferences().getString("clock","")
            return Gson().fromJson(String, RestAlarmClock::class.java)
        }else{
            setDefaultRestAlarm()
            return getSavedClock()
        }
    }

    fun setDefaultRestAlarm() {
        val clock = RestAlarmClock(60,10,true)
        clock.id=0
        sharedPreferences().edit {
            putString("clock",Gson().toJson(clock))
        }
    }
}